// 创建用户相关
import { defineStore } from 'pinia'
import { reqLogin } from '@/api/user'
import type { LoginForm, loginResponseData } from '@/api/user/type'
import type { UserState } from '@/store/modules/types/type'
import { SET_TOKEN, GET_TOKEN } from '@/utils/token'
import { constantRouter } from '@/router/routes'

const useUserStore = defineStore('User', {
  // 存储数据
  state: (): UserState => {
    return {
      token: GET_TOKEN('token'),
      menuRoutes: constantRouter,
      avatar: '',
    }
  },

  actions: {
    async userLogin(data: LoginForm) {
      const result: loginResponseData = await reqLogin(data)
      if (result.code === 200) {
        this.token = result.data.token as string
        SET_TOKEN(this.token)
        return Promise.resolve()
      } else {
        return Promise.reject(new Error(result.data.message))
      }
    },
  },
})

export default useUserStore
